﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
using HouseAgencySite.Models;
using HouseAgencySite.Utils;
using Webdiyer.WebControls.Mvc;

namespace HouseAgencySite.Areas.Admin.Controllers
{
   
    public class HouseInfoController : BaseController
    {
        private AgencyDBContext db = new AgencyDBContext();

        //
        // GET: /Admin/HouseInfo/
        [CheckLogin()]
        public ActionResult Index(int pageindex = 1)
        {
            List<HouseInfos> list = db.HouseInfos.Where(o => o.IsDelete == false).OrderByDescending(o=>o.ModifyTime).ToList();
            if (CurrentUser.UserType == 1)
            {
                list = list.Where(o => o.CreateUserID == CurrentUser.Guid).ToList();
            }
            IPagedList pageList = list.ToPagedList(pageindex, 10);
            return View(pageList);
        }

        //
        // GET: /Admin/HouseInfo/Details/5
        [CheckLogin()]
        public ActionResult Details(string id = null)
        {
            HouseInfos houseinfos = db.HouseInfos.Find(id);
            if (houseinfos == null)
            {
                return HttpNotFound();
            }
            return View(houseinfos);
        }

        //
        // GET: /Admin/HouseInfo/Create
        [CheckLogin()]
        public ActionResult Create()
        {
            HouseInfos houseinfos = new HouseInfos();
            houseinfos.Guid = Guid.NewGuid().ToString();
            ViewBag.Title = "新增房源信息";
            return View(houseinfos);
        }

        //
        // POST: /Admin/HouseInfo/Create

        [HttpPost]
        [CheckLogin()]
        [ValidateInput(false)]
        public ActionResult Create(HouseInfos houseinfos)
        {
            if (ModelState.IsValid)
            {
                houseinfos.Create(houseinfos, CurrentUser);
                db.HouseInfos.Add(houseinfos);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(houseinfos);
        }

        //
        // GET: /Admin/HouseInfo/Edit/5
       
        public ActionResult Edit(string id = null)
        {
            HouseInfos houseinfos = db.HouseInfos.Find(id);
            if (houseinfos == null)
            {
                return HttpNotFound();
            }
            ViewBag.Title = "编辑房源信息";
            return View(houseinfos);
        }

        //
        // POST: /Admin/HouseInfo/Edit/5
        [ValidateInput(false)]
        [HttpPost]
      
        public ActionResult Edit(HouseInfos houseinfos)
        {
            if (ModelState.IsValid)
            {
                db.Entry(houseinfos).State = EntityState.Modified;
                houseinfos.Edit(db, houseinfos, CurrentUser);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(houseinfos);
        }

        //
        // GET: /Admin/HouseInfo/Delete/5
        [CheckLogin()]
        public ActionResult Delete(string id = null)
        {
            HouseInfos houseinfos = db.HouseInfos.Find(id);
            if (houseinfos == null)
            {
                return HttpNotFound();
            }
            return View(houseinfos);
        }

        //
        // POST: /Admin/HouseInfo/Delete/5
        [CheckLogin()]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            HouseInfos houseinfos = db.HouseInfos.Find(id);
            db.HouseInfos.Remove(houseinfos);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult Search(HouseSearchFilter filter)
        {
            var list = db.HouseInfos.Where(o => o.IsDelete == false) as IQueryable<HouseInfos>;
            if (CurrentUser.UserType == 1) {
                list = list.Where(o => o.CreateUserID == CurrentUser.Guid);
            }
            if (!string.IsNullOrEmpty(filter.AreaID))
            {
                list = list.Where(o => o.AreaID == filter.AreaID);
            }
            if (filter.HouseType != 0)
            {
                list = list.Where(o => o.HouseType == filter.HouseType);
            }
            if (!string.IsNullOrEmpty(filter.Title))
            {
                list = list.Where(o => o.Title.Contains(filter.Title));
            }
            if (filter.HuXing != 0)
            {
                list = list.Where(o => o.HuXing == filter.HuXing);
            }
            if (filter.PriceLeft > 0)
            {
                list = list.Where(o => o.Price >= filter.PriceLeft);
            }
            if (filter.MianJiLeft > 0)
            {
                list = list.Where(o => o.MianJi >= filter.MianJiLeft);
            }
            if (filter.PriceRight > 0)
            {
                list = list.Where(o => o.Price <= filter.PriceRight);
            }
            if (filter.MianJiRight > 0)
            {
                list = list.Where(o => o.MianJi < filter.MianJiRight);
            }
            list = list.OrderByDescending(o => o.ModifyTime);
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            return View("Index", list.ToList().ToPagedList(pageIndex, 10));
        }

    }
}